package www.woniuxy.com.class112.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
//import www.woniuxy.com.class112.dao.AgriculturalMarketMapper;
//import www.woniuxy.com.class112.entity.AgriculturalMarket;
//import www.woniuxy.com.class112.entity.AgriculturalMarketExample;
//import www.woniuxy.com.class112.entity.Issues;
//import www.woniuxy.com.class112.entity.SpecialAgents;
import www.woniuxy.com.class112.dao.AgriculturalMarketMapper;
import www.woniuxy.com.class112.entity.AgriculturalMarket;
import www.woniuxy.com.class112.entity.AgriculturalMarketExample;
import www.woniuxy.com.class112.param.insertProductInfoParam;
import www.woniuxy.com.class112.param.selectProductParam;
import www.woniuxy.com.class112.service.ProductService;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    private AgriculturalMarketMapper agriculturalMarketMapper;
    @Override
    public void insertProductInfo(insertProductInfoParam param) throws Exception{
        AgriculturalMarketExample example = new AgriculturalMarketExample();
        AgriculturalMarketExample.Criteria c1 = example.createCriteria();
        c1.andStateIdEqualTo(0L);
        List<AgriculturalMarket> list = agriculturalMarketMapper.selectByExample(example);
        if(list.size()>0&&!list.get(0).getStateId().equals(0L))throw new Exception("该农产品已上架");
        AgriculturalMarket market = BeanUtil.toBean(param, AgriculturalMarket.class);
        agriculturalMarketMapper.insert(market);
    }

    @Override
    public void enableProduct(Long productId) throws Exception{
        AgriculturalMarket market = agriculturalMarketMapper.selectByPrimaryKey(productId);
        if(market==null){
            throw new Exception("农产品不存在");
        }
        if(market.getStateId()!=1L){
            throw new Exception("农产品状态异常");
        }
        market.setStateId(0L);
        agriculturalMarketMapper.updateByPrimaryKey(market);
    }

    @Override
    public PageInfo<AgriculturalMarket> selectProduct(selectProductParam param) throws Exception {
        Page<AgriculturalMarket> page = PageHelper.startPage(param.getPageIndex()==null?1:param.getPageIndex(), param.getPageSize()==null?5:param.getPageSize());
        AgriculturalMarketExample example = new AgriculturalMarketExample();
        AgriculturalMarketExample.Criteria criteria = example.createCriteria();
        criteria.andStateIdNotEqualTo(2L);
        if (param.getProductName()!=null){
            criteria.andProductNameLike("%"+param.getProductName()+"%");
        }
        if (param.getStateId()!=null){
            criteria.andStateIdEqualTo(param.getStateId());
        }
        agriculturalMarketMapper.selectByExample(example);
//        agriculturalMarketMapper.selectProduct(BeanUtil.toBean(param,AgriculturalMarket.class));
        return new PageInfo<AgriculturalMarket>(page) ;
    }

    @Override
    public void disableProduct(Long productId) throws Exception {
        AgriculturalMarket market = agriculturalMarketMapper.selectByPrimaryKey(productId);
        if(market==null){
            throw new Exception("农产品不存在");
        }
        if(market.getStateId()!=0L){
            throw new Exception("农产品状态异常");
        }
        market.setStateId(1L);
        agriculturalMarketMapper.updateByPrimaryKey(market);
    }

    @Override
    public void redactProduct(AgriculturalMarket form)throws Exception{
        AgriculturalMarket market = agriculturalMarketMapper.selectByPrimaryKey(form.getProductId());
        if(market==null){
            throw new Exception("农产品不存在");
        }
        agriculturalMarketMapper.updateByPrimaryKeySelective(form);
    }

    @Override
    public void deleteProduct(Long productId) throws Exception {
        AgriculturalMarket market = agriculturalMarketMapper.selectByPrimaryKey(productId);
        if(market==null){
            throw new Exception("农产品不存在");
        }
        if (market.getStateId()==2){
            throw new Exception("该农产品已删除");
        }
        market.setStateId(2L);
        agriculturalMarketMapper.updateByPrimaryKeySelective(market);
    }

    @Override
    public PageInfo<AgriculturalMarket> userselectProduct(selectProductParam param) {
        Page<AgriculturalMarket> page = PageHelper.startPage(param.getPageIndex()==null?1:param.getPageIndex(), param.getPageSize()==null?5:param.getPageSize());
        AgriculturalMarketExample example = new AgriculturalMarketExample();
        AgriculturalMarketExample.Criteria criteria = example.createCriteria();
        criteria.andStateIdEqualTo(0L);
        if (param.getProductName()!=null){
            criteria.andProductNameLike("%"+param.getProductName()+"%");
        }
        if (param.getStateId()!=null){
            criteria.andStateIdEqualTo(param.getStateId());
        }
        agriculturalMarketMapper.selectByExample(example);
//        agriculturalMarketMapper.selectProduct(BeanUtil.toBean(param,AgriculturalMarket.class));
        return new PageInfo<AgriculturalMarket>(page) ;
    }
}
